package com.yhl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.yhl.entity.Bill;
import com.yhl.entity.Provider;
import com.yhl.mapper.BillMapper;
import com.yhl.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpSession;

/**
 * @author wangxiao
 * @since 2023-03-17 16:50:41
 */

@Controller
public class ProviderController {
    @Autowired
    HttpSession session;

    @Autowired
    private ProviderService providerService;

    @Autowired
    private BillMapper billMapper;

    @RequestMapping("/providerlist.do")
    public String providerlist(String pageIndex,String queryProCode , String queryProName) {
        int  currentPageNo = pageIndex==null?1:Integer.valueOf(pageIndex);
        Page page = Page.of(currentPageNo,5);
        Provider provider = new Provider();
        provider.setProCode(queryProCode);
        provider.setProName(queryProName);
        IPage<Provider> providerIPage =  providerService.getProviderByPage(page,provider);
        queryProCode = queryProCode == null ? "" : queryProCode;
        queryProName = queryProName == null ? "" : queryProName;
        session.setAttribute("queryProCode",queryProCode);
        session.setAttribute("queryProName",queryProName);
        session.setAttribute("providerList",providerIPage.getRecords());
        session.setAttribute("totalPageCount",providerIPage.getPages());
        session.setAttribute("totalCount",providerIPage.getTotal());
        session.setAttribute("currentPageNo",currentPageNo);
        return "providerlist";
    }

    @RequestMapping("/addprovider.do")
    public String addprovider(Provider provider){
        //对象  ===>HashMap
        provider.setCreationDate(new Date());
        providerService.addProvider(provider);
        return   "redirect:/providerlist.do";
    }


    @RequestMapping("/jsp/delprovider.do")
    @ResponseBody
    public HashMap<String,String> deleteProvider(int proid){
        LambdaQueryWrapper<Bill> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Bill::getProviderId,proid);
        List<Bill> list = billMapper.selectList(lambdaQueryWrapper);
        HashMap<String,String> resultMap = new HashMap<>();
        if(list.size()<=0){
            providerService.deleteProvider(String.valueOf(proid));
            resultMap.put("delResult","true");
        }else{
            resultMap.put("delResult", String.valueOf(list.size()));
        }
        return resultMap;
    }


    @RequestMapping("/jsp/viewprovider.do")
    public String viewprovider(String method,String proid){
        //对象  ===>HashMap
        System.out.println(method);
        System.out.println(proid);
        Provider provider = providerService.getProviderById(proid);
        session.setAttribute("provider",provider);
        if(method.equals("view")){
            return   "providerview";
        }else {
            return   "providermodify";
        }
    }
    @RequestMapping("jsp/updprovider.do")
    public String updprovider(Provider provider){
        providerService.updateProvider(provider);
        return   "redirect:/providerlist.do";
    }


}